%% Fig 1A
load('./data/Fig1A.mat')

myfun_CW =@(c,x) c(4)- c(1).*sinc((x-c(2))/c(3)).^2; % change to sinc funciton by Mo Chen 11/17/2017
gamma=(x(end)-x(1))/4;%Initial guess of linewidth. Modified by Mo Chen 03/02/2015
cont=max(y)-min(y);
[~,f]=min(y);%Initial guess of resonant frequency
f=x(f);
pinit=[cont,f,gamma,y(1)-myfun_CW([cont,f,gamma,0],x(1))];

LB=[-2,x(1),gamma/1e4,-10];
UB=[2,x(end),gamma*1e2,10];
[pbest]=easyfit(x, y, pinit, myfun_CW, LB, UB,'np');

y_fit=myfun_CW(pbest,x);
cc=colormap(piratepal(9,1));
plot(x*1e-6,y_fit,'LineWidth',1.5,'Color',cc(1,:));
hold on
errorbar(x*1e-6,y,dy,'o','LineWidth',2,'MarkerSize',12,'Color',cc(1,:));

xlabel('Modulation Frequency (MHz)','FontSize',18)
ylabel('Population (a.u.)','FontSize',18)
ylim([-0.05,1.1])

set(gca,'FontSize',18)

%% Fig 1B
load('./data/Fig1B.mat')
myfun_rabi_base =@(c,x) c(3)+ c(2)*cos(2*pi*x*c(1)+c(4));
% Rabi SQ
% [1,0,0]
myfun_rabi = @(c,x) myfun_rabi_base([c,pi],x);
LB=[0.1e6,0,-1];
UB=[0.3e6,1,1];
pinit=[0.2e6,0.25,0.25];

pbest=easyfit(x1, y1, pinit, myfun_rabi, LB, UB,'np');
y1_fit=myfun_rabi(pbest,x1);
figure
plot(x1*1e6,y1_fit,'LineWidth',1.5,'Color',cc(1,:),'HandleVisibility','off')
hold on
errorbar(x1*1e6,y1,dy1,'o','MarkerSize',12,'LineWidth',1.5,'Color',cc(1,:))


% Rabi DQ
% [0,1,1]
myfun_rabi = @(c,x) myfun_rabi_base([c,0],x);
LB=[0.1e6,0,-1];
UB=[0.3e6,1,1];
pinit=[0.2e6,0.5,0.5];

pbest=easyfit(x2, y2, pinit, myfun_rabi, LB, UB,'np');
y2_fit=myfun_rabi(pbest,x2);
plot(x2*1e6,y2_fit,'LineWidth',1.5,'Color',cc(2,:),'HandleVisibility','off')
hold on
errorbar(x2*1e6,y2,dy2,'o','MarkerSize',12,'LineWidth',1.5,'Color',cc(2,:))

% [0,1,-1]
myfun_rabi = @(c,x) myfun_rabi_base([c,0],x);
LB=[0.1e6,0,-1];
UB=[0.3e6,1,1];
pinit=[0.2e6,0.5,0.5];

pbest=easyfit(x3, y3, pinit, myfun_rabi, LB, UB,'np');
y3_fit=mean(y3)*ones(size(x3));
plot(x3*1e6,y3_fit,'LineWidth',1.5,'Color',cc(3,:),'HandleVisibility','off')
hold on
errorbar(x3*1e6,y3,dy3,'o','MarkerSize',12,'LineWidth',1.5,'Color',cc(3,:))

xlabel('Duration of Resonant Modulation (\mus)','FontSize',18)
ylabel('Population (a.u.)','FontSize',18)
% ylim([-0.05,1.05])

legend({'\Gamma_{\alpha,\alpha}','\Gamma_{\beta,\phi}','\Gamma_{\beta,-\phi}'},'FontSize',18,'Location','best')

set(gca,'FontSize',18)



